package com.yourcom.proname.biz.filter;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.filter.OncePerRequestFilter;

import javax.servlet.FilterChain;
import javax.servlet.ReadListener;
import javax.servlet.ServletException;
import javax.servlet.ServletInputStream;
import javax.servlet.annotation.WebFilter;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletRequestWrapper;
import javax.servlet.http.HttpServletResponse;
import java.io.BufferedReader;
import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStreamReader;


/**
 * 记录post请求中body信息到日志里
 */
@WebFilter
public class PostParamsLogFilter extends OncePerRequestFilter {

    final static private Logger logger = LoggerFactory.getLogger(PostParamsLogFilter.class);

    @Override
    protected void doFilterInternal(HttpServletRequest request, HttpServletResponse response, FilterChain filterChain) throws ServletException, IOException {
        if (request.getMethod().equals("POST") && request.getContentType().equals("application/json")) {
            BufferedReader br = request.getReader();
            String str;
            StringBuilder wholeStr = new StringBuilder();
            while ((str = br.readLine()) != null) {
                wholeStr.append(str.trim());
            }
            logger.info(request.getRequestURL().append(":").append(wholeStr.toString()).toString());
            br.close();
            PostReplaceServletRequestWrapper postReplaceServletRequestWrapper = new PostReplaceServletRequestWrapper(request);
            postReplaceServletRequestWrapper.setBody(wholeStr.toString());
            filterChain.doFilter(postReplaceServletRequestWrapper, response);
        } else {
            filterChain.doFilter(request, response);
        }
    }

    static class PostReplaceServletRequestWrapper extends HttpServletRequestWrapper {
        private String body;

        public void setBody(String body) {
            this.body = body;
        }

        /**
         * Constructs a request object wrapping the given request.
         *
         * @param request The request to wrap
         * @throws IllegalArgumentException if the request is null
         */
        public PostReplaceServletRequestWrapper(HttpServletRequest request) {
            super(request);
        }

        @Override
        public ServletInputStream getInputStream() throws IOException {
            ByteArrayInputStream byteArrayInputStream = new ByteArrayInputStream(body.getBytes());
            return new ServletInputStream() {
                @Override
                public boolean isFinished() {
                    return false;
                }

                @Override
                public boolean isReady() {
                    return false;
                }

                @Override
                public void setReadListener(ReadListener listener) {

                }

                @Override
                public int read() throws IOException {
                    return byteArrayInputStream.read();
                }
            };
        }

        @Override
        public BufferedReader getReader() throws IOException {
            return new BufferedReader(new InputStreamReader(this.getInputStream()));
        }
    }
}

